package utils;

import structure.ListenerNode;
import structure.Transcation;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author zhangjianzhong
 * @create 2021-11-21 20:43
 */
public class FileAndPrintUtils {
    public static void printArray(Object[] obj) {
        for (Object o : obj) {
            System.out.print(o + " ");
        }
        System.out.println("\n");
    }

    /**
     * 将交易和发送方记录到scv文件中
     * 暂且将路径写死吧，
     */
    public void TransactionFromToSCVFile(String filename,HashMap<String, Transcation> transactionMap) {

        boolean succ = createFile(filename);
        if (succ) {
            try {
                FileWriter fileWriter = new FileWriter(filename);
                fileWriter.write("transactonHash,fromID\n");
                for (Transcation en : transactionMap.values()) {
                    fileWriter.write(en.transHash + "," + en.fromID + "\n");
                }
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void FileStoreOfArray(String filename, ArrayList<String> arrayList){
        boolean succ= createFile(filename);
        if(succ){
            try {
                FileWriter fileWriter= new FileWriter(filename);
                for (int i=0;i<arrayList.size();i++){
                    fileWriter.write(arrayList.get(i)+"\n");
                }
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }



    public static void PrintSystem(Map<String, ListenerNode> listenerNodeMap, HashMap<String, Transcation> transactionMap, HashSet<String> normalNodeSet) {

        System.out.println("监听节点共计：" + listenerNodeMap.size() + "个");
        System.out.println("监听节点收集转发交易情况：");
        for (Map.Entry<String, ListenerNode> entry : listenerNodeMap.entrySet()) {
            System.out.println("监听节点 " + entry.getKey() + " :");
            System.out.println("\t邻居共计：" + entry.getValue().neightourHas.size());
            System.out.println("\t监听交易共计：" + entry.getValue().txListenSet.size());
        }
        System.out.println("产生交易共计：" + transactionMap.size() + "笔");
        System.out.println("普通节点共计：" + normalNodeSet.size() + "个");
    }


    public static boolean createFile(String filename) {
        try {
            File file = new File(filename);
            file.createNewFile();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }

    public void writeToFile(String filename, double[][] Array) {
        createFile(filename);
        try {
            FileWriter fileWriter = new FileWriter(filename);
            for (double[] arr : Array) {
                for (double value : arr) {
                    fileWriter.write(value + "\t");
                }
                fileWriter.write("\n");
            }
            fileWriter.close();
            //  return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //FileAndPrintUtils fapU = new FileAndPrintUtils();
        HashMap<Integer, String> t = new HashMap<>();
        t.put(1, "dd");
        t.put(2, "34");
        createFile("1.txt");
        try {
            FileWriter fileWriter = new FileWriter("1.txt");
            for (String s : t.values()) {
                fileWriter.write(s + "\n");
            }
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
